package com.xiguaji.tiktok.v1.tiktok.aweme.controller;
import com.xiguaji.tiktok.config.exception.ParamsException;
import com.xiguaji.tiktok.utils.Ret;
import com.xiguaji.tiktok.v1.BaseController;
import com.xiguaji.tiktok.v1.tiktok.aweme.dto.SearchData;
import com.xiguaji.tiktok.v1.tiktok.aweme.service.AwemeService;
import com.xiguaji.tiktok.v1.tiktok.permission.Limit;
import com.xiguaji.tiktok.v1.tiktok.permission.RequestLimit;
import com.xiguaji.tiktok.v1.tiktok.permission.RequestLimits;
import com.xiguaji.tiktok.v1.user.log.Operation;
import com.xiguaji.tiktok.v1.user.log.ParamsType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

@RestController
@Api(value = "视频",tags = {"视频"})
public class AwemeController  extends BaseController {
    @Autowired
    AwemeService awemeService;

    @ApiOperation(value = "获取视频列表信息",notes = "获取视频分页列表信息\n" +
            "排序sort参数说明：\n" +
            "1. ALL：按传播指数排序\n" +
            "2. Views：按播放数排序\n" +
            "3. Likes：按点赞数排序\n" +
            "4. Reviews：按评论数排序\n" +
            "5. Share：按分享数排序" +
            "%7B%22page%22%3A1%2C%22pageSize%22%3A10%2C%22createDate%22%3A4%7D")
    @ApiResponses(value = {
            @ApiResponse(code = 200,message = "获取成功")
    })
    @RequestMapping(value = "aweme",method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @RequestLimits({
            @RequestLimit(value = "AwemePropertyLimit",property = "createDate",validateType = Limit.PROPERTY_LIMIT),
            @RequestLimit(value = "PageDataLimit",property = "page,pageSize",validateType = Limit.VIEW_DATA_LIMIT)

    })
    @Operation(value = "获取视频列表信息")
    public String getAwemeList(String data) throws IOException, ParamsException {
       SearchData searchData = getParam(SearchData.class, URLDecoder.decode(data,"UTF-8"));
        return awemeService.getPageEs(searchData,getUser().getId());
    }

    @ApiOperation(value = "获取视频评论")
    @RequestMapping(value = "aweme/comment",method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @Operation(value = "获取视频评论",fetchParam = "awemeId",paramType = ParamsType.JSON)
    public String getAwemeCommentList(String data) throws ParamsException, UnsupportedEncodingException {
        Long awemeId = getParam(Long.class,URLDecoder.decode(data,"UTF-8"),"awemeId");
        int page = getParam(Integer.class,URLDecoder.decode(data,"UTF-8"), "page");
        int pageSize = getParam(Integer.class,URLDecoder.decode(data,"UTF-8"),"pageSize");
        return awemeService.getAwemeComments(awemeId,page,pageSize);
    }

    @ApiOperation(value = "获取视频详情")
    @RequestMapping(value = "aweme/{awemeId}",method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @Operation(value = "获取视频详情",fetchParam = "awemeId",paramType = ParamsType.URL)
    public String getAwemeDetail(@PathVariable long awemeId) throws IOException {
        return Ret.success("OK",awemeService.getAwemeDetailEs(awemeId,getUser().getId()));
    }
}
